/**
 * <p>Title: TagDao.java</p>
 * <p>Description: DoNotes</p>
 * @author WangJun
 * @date 2009/11/19 23:02:31
 * @version 1.0
 */
package net.donotes.blog.dao;

import java.util.ArrayList;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import net.donotes.blog.pojo.Tag;
import net.donotes.util.dao.PMF;

/**
 * @author WangJun
 *
 */
public class TagDao {
	/**
	 * Title: addTag
	 * @param tag
	 */
	public void addTag(Tag tag) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
        try {
            pm.makePersistent(tag);
        } finally {
          //  pm.close();
        }
	}
	
	/**
	 * Title: delTag
	 * @param tag
	 */
	public void delTag(Tag tag) {
		// TODO Auto-generated method stub
	}
	
	/**
	 * 
	 * Title: getTag
	 * @param tagName
	 */
	public Tag getTag(String tagName) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query query = pm.newQuery(Tag.class);
	    query.setFilter("name == nameParam");
		query.declareParameters("String nameParam");

		List<Tag> resultes = (List<Tag>)query.execute(tagName);
		if (resultes.iterator().hasNext()) {
			return resultes.get(0);
		} else {
			return null;
		}	
	}
	
	/**
	 * Title: updTag
	 * @param tag
	 */
	public void updTag(Tag tag) {
	    PersistenceManager pm = PMF.get().getPersistenceManager();
	    try {
	    	Tag t = pm.getObjectById(Tag.class, tag.getName());
	            t.setFrequency(tag.getFrequency());
	    } finally {
	        pm.close();
	    }
	}
	
	/**
	 * Title: listTag
	 * @return
	 */
	public List<Tag> listTag() {
		List<Tag> tags = new ArrayList<Tag>();
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query query = pm.newQuery(Tag.class);
	    try {
	    	tags = (List<Tag>) query.execute();
/*	    	
	    	if (posts.iterator().hasNext()) {
	    		
	            for (Post e : posts) {
	            	pm.deletePersistent(e);	
	            }	    		
	    	}
*/	    		    	
	    } finally {
	        query.closeAll();
	        //pm.close();
	    }
	    
		return tags;
	}
}
